#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright © 2005-2007 Kaufmann Manuel
#
# This file is part of Tweety-Finger.
#
# Tweety-Finger is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Tweety-Finger is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Tweety-Finger; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  
# USA

import pygame
from pygame.locals import *
import random



class Window(object):
	def __init__(self, title="", size=(640, 480), bgImage="", icon=""):
		"""
			Inicializador de la clase. Crea la ventana y
			pone el título
		"""
		
		pygame.init()
		pygame.display.set_icon(pygame.image.load(icon))
		
		
		
		# Imágenes
		self.backgroundPos = 0
		self.backgrounds = ["image/derecha.png",   \
				"image/izquierda.png", \
				"image/main.png", \
				"image/menu.jpg"]
		self.images = []
		for x in self.backgrounds:
			self.images.append(pygame.image.load(x))
		
		# Colores
		self.black = (10, 10, 10)
		self.red = (255, 0, 0)
		self.grey = (170, 170, 170)
		
		# Caracteres aceptado
		self.acceptValues = "abcdefghijklmnopqrstuvwxyz0123456789 "
		
		# Pantalla
		self.screen = pygame.display.set_mode(size)
		
		# Fuentes
		self.font = pygame.font.Font("fonts/acquaint.ttf", 25)
		self.letter = pygame.font.Font("fonts/fanboyhcb.ttf", 70)
		self.fontNameScore = pygame.font.Font("fonts/fanboyhcb.ttf", 25)
		
		# Sonidos
		self.goodEnter = pygame.mixer.Sound("sound/good-enter.ogg")
		self.badEnter = pygame.mixer.Sound("sound/bad-enter.wav")
		self.clock = pygame.mixer.Sound("sound/clock.ogg")
		
		# Titulo de la aplicación
		self.mainTitle = "How fast are you?"
		self.setApplicationTitle()
		self.setTitle(self.mainTitle)
	
	def setApplicationTitle(self):
		"""
			Pone el título de la aplicación
		"""
		
		pygame.display.set_caption("Tweety Finger")
	
	def setBackground(self, option, backImage=None):
		"""
			Setea la imagen de fondo del juego
		"""
		
		
		if option == "main":
			# Si queremos que muestre la imagen principal
			backImage = self.images[2]
		elif option == "game":
			self.backgroundPos += 1
			backImage = self.images[self.backgroundPos%2]
		elif option == "menu":
			backImage = self.images[3]
		self.screen.fill(self.black)
		self.screen.blit(backImage, backImage.get_rect())
		pygame.display.flip()
		
	def setTitle(self, title):
		"""
			Setea el string pasado por argumento como título
		"""
		
		text = self.font.render(title, 1, self.red)
		textpos = text.get_rect()
		textpos.centerx = self.screen.get_rect().centerx
		self.screen.blit(text, textpos)
		pygame.display.flip()

	def setLetter(self, letter):
		"""
			Muestra la letra letter en el centro de
			la computadora
		"""
		
		color = (random.randint(0, 255), \
			random.randint(0, 255), \
			random.randint(0, 255)) 
	
		shadow = self.letter.render(letter, 1, self.grey)
		shadowpos = shadow.get_rect()
		shadowpos.centerx = self.screen.get_rect().centerx - 80
		shadowpos.centery = 100
	
		letter = self.letter.render(letter, 1, color)
		letterpos = letter.get_rect()
		letterpos.centerx = self.screen.get_rect().centerx - 75
		letterpos.centery = 95
	
		self.screen.blit(shadow, shadowpos)
		self.screen.blit(letter, letterpos)
	
		pygame.display.flip()

	def showOnePosition(self, position, name, time, scoreFont):
		"""
			Muestra una posición del high scores
		"""
		
		text = "%-4s%-11s%3.5f" % (position, name, float(time))
		text = scoreFont.render(text, 1, self.black)
		textpos = text.get_rect()
		textpos.centerx = self.scorePositionx
		textpos.centery = self.scorePositiony
		self.scorePositiony += self.scoreSpacey
		self.screen.blit(text, textpos)
		pygame.display.flip()

	def showMyScore(self, time):
		"""
			Muestra el score del jugador actual, cuando
			termina la cadena de forma correcta
		"""
		
		self.setBackground("main")
		text = self.font.render("Your score:", 1, self.black)
		textpos = text.get_rect()
		textpos.centerx = self.screen.get_rect().centerx - 75
		textpos.centery = 80
		self.screen.blit(text, textpos)
		
		text = self.font.render(time, 1, self.red)
		textpos = text.get_rect()
		textpos.centerx = self.screen.get_rect().centerx - 75
		textpos.centery = 110
		self.screen.blit(text, textpos)
		pygame.display.flip()
		
	def setHighScorePosition(self, xpos, ypos, scoreSpacey):
		self.scorePositiony = ypos
		self.scorePositionx = xpos
		self.ypos = ypos
		self.xpos = xpos
		self.scoreSpacey = scoreSpacey
	
	def resetHighScorePosition(self):
		self.scorePositiony = self.ypos
		self.scorePositionx = self.xpos

	def setNameScore(self, name):
		"""
			Muestra el nombre name en el centro de
			la pantalla con un caracter "_"
		"""
		
		if len(name) != 10:
			name += "_"
		self.setBackground("menu")
		self.setTitle("You won, that's a great score")
		shadow = self.fontNameScore.render(name, 1, self.grey)
		shadowpos = shadow.get_rect()
		shadowpos.centerx = self.screen.get_rect().centerx
		shadowpos.centery = 100
	
		letter = self.fontNameScore.render(name, 1, self.red)
		letterpos = letter.get_rect()
		letterpos.centerx = self.screen.get_rect().centerx - 5
		letterpos.centery = 95
	
		self.screen.blit(shadow, shadowpos)
		self.screen.blit(letter, letterpos)
	
		pygame.display.flip()

	def insertName(self):
		"""
			Espera que el usuario ingrese el nombre
		"""
		
		
		name = ""
		done = False
		self.setBackground("main")
		self.setTitle("You won it's a great score")
		self.setNameScore("")
		while done == False:
			event = pygame.event.wait()
			eventType = event.type
			if eventType == KEYDOWN:
				if event.key in [K_RETURN, K_KP_ENTER]:
					done = True
				letter = event.unicode
				if (letter in self.acceptValues or letter in self.acceptValues.upper()) and len(name) < 10:
					name += letter
					self.setNameScore(name)
		return name